<?php

require_once 'User.php';
class Application_Model_DbTable_Notice extends Zend_Db_Table_Abstract
{
    protected $_name = 'ojnotice';

    public function addNotice(Application_Model_Notice $notice){
        $data = array(
            'ojuId' => $notice->getUser()->getId(),
            'ojnTitle' => $notice->getTitle(),
            'ojnContent' => $notice->getContent(),
            'ojnTime' => $notice->getTime(),
            'ojnMemo' => $notice->getMemo()
        );
        $this->insert($data);
        return $this;
    }

    public function updateNotice(Application_Model_Notice $notice){
        $data = array(
            'ojuId' => $notice->getUser()->getId(),
            'ojnTitle' => $notice->getTitle(),
            'ojnContent' => $notice->getContent(),
            'ojnTime' => $notice->getTime(),
            'ojnMemo' => $notice->getMemo()
        );
        $this->update($data, 'ojnId = '.$notice->getId());
//        return $this;
    }
    
    public function deleteNotice($id){
        $this->delete('ojnId = '.$id);
        return $this;
    }
    
    public function getNoticeById($id){
        $row = $this->fetchRow('ojnId = '.$id);
        if(!$row)
            return null;
        $u = new Application_Model_DbTable_User();

//        return new Application_Model_Notice($id, $u->getUserById($row->ojuId),
//                $row->ojnTitle,$row->ojnContent,$row->ojnTime,$row->ojnMemo);
        
        $notice = new Application_Model_Notice();
        $notice->setId($id);
        $notice->setUser($u->getUserById($row->ojuId));
        $notice->setTitle($row->ojnTitle);
        $notice->setContent($row->ojnContent);
        $notice->setTime($row->ojnTime);
        $notice->setMemo($row->ojnMemo);
        
        return $notice;
    }
    
    public function getNotices(){
        $rowset = $this->fetchAll();
        $notices = array();
        $u = new Application_Model_DbTable_User();
        foreach ($rowset as $row) {
//            $notice = new Application_Model_Notice($row->ojnId, $u->getUserById($row->ojuId),
//                $row->ojnTitle,$row->ojnContent,$row->ojnTime,$row->ojnMemo);
            
            $notice = new Application_Model_Notice();
            $notice->setId($row->ojnId);
            $notice->setUser($u->getUserById($row->ojuId));
            $notice->setTitle($row->ojnTitle);
            $notice->setContent($row->ojnContent);
            $notice->setTime($row->ojnTime);
            $notice->setMemo($row->ojnMemo);
            
            $notices[] = $notice;
        }
        return $notices;
    }
    
    public function getNoticesByRange($start, $offset){
        $rowset = $this->fetchAll(null,'ojnid DESC',$offset,$start);
        $notices = array();
        $u = new Application_Model_DbTable_User();
        foreach ($rowset as $row) {
            
            $notice = new Application_Model_Notice();
            $notice->setId($row->ojnId);
            $notice->setUser($u->getUserById($row->ojuId));
            $notice->setTitle($row->ojnTitle);
            $notice->setContent($row->ojnContent);
            $notice->setTime($row->ojnTime);
            $notice->setMemo($row->ojnMemo);
            
            $notices[] = $notice;
        }
        return $notices;
    }
    
    public function countNotices() {
        $select = $this->select();
        $select->from($this->_name,'COUNT(ojnId) AS num');
        return $this->fetchRow($select)->num;
    }
}

